package com.example.moviecms2.controller;

import com.example.moviecms2.domain.Actor;
import com.example.moviecms2.domain.Movie;
import com.example.moviecms2.domain.Role;
import com.example.moviecms2.repository.ActorRepository;
import com.example.moviecms2.repository.MovieRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.lang.model.element.NestingKind;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/actor")
public class ActorController {

    @Autowired
    private MovieRepository movieRepository;

    @Autowired
    private ActorRepository actorRepository;

    @RequestMapping(value = "/index")
    public String index(Model model){
        List<Actor> list = actorRepository.findAll();
        model.addAttribute("list",list);
        return "actor/index";
    }
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public  String toadd(){
        return "actor/add";
    }
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String doadd(@RequestParam String name,@RequestParam String sex,@RequestParam String born){

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

       Date date = null;
        try {
            date = format.parse(born);
        } catch (Exception e) {
        }

        Integer intsex = null;
        try {
            intsex = Integer.valueOf(sex);
        } catch (Exception e) {
        }

        Actor actor = new Actor();
        actor.setName(name);
        actor.setSex(intsex);
        actor.setBorn(date);
        actorRepository.save(actor);
        return "redirect:/actor/index";
    }

    //toEdit()方法跳转到编辑页面
    @RequestMapping(value = "/edit/{actorId}",method = RequestMethod.GET)
    public String toEdit(Model model, @PathVariable Long actorId){
        Actor actor = actorRepository.findOne(actorId);
        model.addAttribute("info",actor);
        return "actor/edit";
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String doEdit(@RequestParam Long id,@RequestParam String name,@RequestParam String sex,@RequestParam String born){

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Date date = null;
        try {
            date = format.parse(born);
        } catch (Exception e) {
        }

        Integer intsex = null;
        try {
            intsex = Integer.valueOf(sex);
        } catch (Exception e) {
        }
        //找到演员Id
       Actor actor = actorRepository.findOne(id);
        //设置演员信息
        actor.setName(name);
        actor.setSex(intsex);
        actor.setBorn(date);
        actorRepository.save(actor);
        return "redirect:/actor/index";
    }

    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public String delete(@PathVariable Long id){
        Actor actor = actorRepository.findOne(id);
        actorRepository.delete(id);
        return "redirect:/actor/index";
    }

}
